// 加载Express模块
const express = require('express');

// 加载MySQL模块
const mysql = require('mysql');

// 加载bodyParser模块
const bodyParser = require('body-parser');

// 加载MD5模块
const md5 = require('md5');

// 创建MySQL连接池
const pool = mysql.createPool({
    host: '127.0.0.1', //MySQL服务器地址
    port: 3306, //MySQL服务器端口号
    user: 'root', //数据库用户的用户名
    password: '', //数据库用户密码
    database: 'gameshop', //数据库名称
    connectionLimit: 20, //最大连接数
    charset: 'utf8' //数据库服务器的编码方式
});


const server = express();

server.use(bodyParser.urlencoded({
    extended: false
}));



const cors = require('cors');


server.use(cors({
    origin: ['http://localhost:8080', 'http://192.168.0.111:8080']
}));

server.get('/fl', (req, res) => {
    let tag = req.query.tag

    tag = '%' + tag + '%'

    let sql = 'select * from game where gtag like ?'
    pool.query(sql, [tag], (error, results) => {
        if (error) throw error;
        res.send({ message: 'ok', code: 200, results: results });
    })
})
server.get('/details', (req, res) => {
    let id = req.query.id
    gid = parseInt(id)
    let sql = 'select * from game where  gid=?'
    pool.query(sql, [gid], (error, results) => {
        if (error) throw error;
        res.send({ message: 'ok', code: 200, results: results });
    })
})
server.get('/game', (req, res) => {

    let sql = 'SELECT * FROM game ORDER BY gid';

    pool.query(sql, (error, results) => {
        if (error) throw error;
        res.send({ message: 'ok', code: 200, results: results });
    });
});
server.get('/goshop', (req, res) => {
    let sql = 'select * from goshop order by gtime '
    pool.query(sql, (error, results) => {
        if (error) throw error;
        res.send({ message: 'ok', code: 200, result: results })
    })
})

server.get('/search', (req, res) => {
    let name = req.query.val
    let gname = '%' + name + '%'
    let sql = "select * from game where gname like ?";
    pool.query(sql, [gname], (error, results) => {
        if (error) throw error;
        res.send({ message: 'ok', code: 200, results: results })

    })
})

server.get('/com', (req, res) => {
    let sql = 'select * from user'
    pool.query(sql, (error, results) => {
        if (error) throw error;
        res.send({ message: 'ok', code: 200, result: results })
    })
})

server.get('/img', (req, res) => {
    let id = req.query.id;
    let sql = "select * from simg where gid=?";
    pool.query(sql, [id], (error, results) => {
        if (error) throw error;
        res.send({ message: 'ok', code: 200, result: results[0] })
    })
})

server.get('/delete', (req, res) => {
    let id = req.query.gid
    gid = parseInt(id)

    let sql = 'delete from goshop where gid=?';
    pool.query(sql, [gid], (error, results) => {
        if (error) throw error;
        res.send({ message: 'ok', code: 200, result: results })
    })
})


server.post('/goshop', (req, res) => {
    // console.log(res)
    // console.log(req);
    let gid = req.body.gid;
    let gimg = req.body.gimg;
    let gname = req.body.gname;
    let gjs = req.body.gjs;
    let gtag1 = req.body.gtag1;
    let gtag2 = req.body.gtag2;
    let gtag3 = req.body.gtag3;
    let gprice = req.body.gprice;
    let gtime = req.body.gtime;
    let sql = 'select count(gid) as count from goshop where gid=?';

    pool.query(sql, [gid], (error, results) => {

        let count = results[0].count;
        if (count == 0) {
            sql = 'insert goshop(gid, gimg, gname, gjs, gtag1,gtag2,gtag3, gprice,gtime) values(?,?,?,?,?,?,?,?,?)';
            pool.query(sql, [gid, gimg, gname, gjs, gtag1, gtag2, gtag3, gprice, gtime], (error, results) => {
                if (error) throw error;
                res.send({ message: 'ok', code: 200 });

            })
        } else {
            res.send({ message: 'user exists', code: 201 });
        }
    })
})

server.post('/register', (req, res) => {

    let account = req.body.account;
    let password = req.body.password;

    let sql = 'SELECT COUNT(id) AS count FROM user WHERE account=?';
    pool.query(sql, [account], (error, results) => {
        if (error) throw error;
        let count = results[0].count;
        if (count == 0) {

            sql = 'INSERT user(account,pwd) VALUES(?,?)';
            pool.query(sql, [account, password], (error, results) => {
                if (error) throw error;
                res.send({ message: 'ok', code: 200 });
            })
        } else {
            res.send({ message: 'user exists', code: 201 });
        }
    });
});



// 用户登录接口
server.post('/login', (req, res) => {
    //获取用户名和密码信息
    let account = req.body.account;
    let password = req.body.password;

    // SQL语句
    let sql = 'SELECT * FROM user WHERE account=? AND pwd=?';
    pool.query(sql, [account, password], (error, results) => {
        if (error) throw error;
        if (results.length == 0) { //登录失败
            res.send({ message: 'login failed', code: 201 });
        } else { //登录成功
            res.send({ message: 'ok', code: 200, result: results[0] });
        }
    });

});

// 指定服务器对象监听的端口号
server.listen(3000, () => {
    console.log('server is running...');
});